Skip to content

Fix memory leaks in addCapability and addConditionalCapability#3403

Merged
MrSidims merged 2 commits intoKhronosGroup:mainfrom
jmmartinez:leak
Oct 22, 2025
Merged

Fix memory leaks in addCapability and addConditionalCapability#3403
MrSidims merged 2 commits intoKhronosGroup:mainfrom
jmmartinez:leak

Conversation

@jmmartinez
Copy link
Contributor

@jmmartinez jmmartinez commented Oct 20, 2025

Compiling with -fsanitize=address uncovered 2 memory leaks related to the use of raw pointers in addCapability and addConditionalCapability.

Instead of manually handling memory, use std::unique_ptr to track the ownership of the pointed object and free the memory for us.

Copy link
Contributor

@MrSidims MrSidims left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, thank you!

Small typo in commit msg: fsantize -> fsanitize

Compiling with -fsanitize=address uncovered 2 memory leaks. Instead of
handling memory manually, we could use std::unique_ptr to free the
memory for us.
Copy link
Contributor

@maarquitos14 maarquitos14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a slightly related offtopic comment.

class SPIRVModule {
public:
typedef std::map<SPIRVCapabilityKind, SPIRVCapability *> SPIRVCapMap;
typedef std::map<SPIRVCapabilityKind, std::unique_ptr<SPIRVCapability>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're here, do we really need to keep the order here? Could this be made std::unordered_map? That would probably give us better performance. Anyway, I know this is offtopic, so feel free to ignore or address in a different patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add it to my TODO list and do it in a separate PR. It's a low hanging fruit so it's worth checking.

I'll also see if I can remove the rest of the manual memory management from the class, such that the destructor becomes trivial.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gave it a try and I know now why std::map is used: If we want to use the unordered containers on these two maps we have to define std::hash(const SPIRVCapabilityKind&).

@MrSidims MrSidims merged commit c11cd5b into KhronosGroup:main Oct 22, 2025
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants